﻿using App_Clinique.ClassLibrary.Classes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tools.Libraries;

namespace App_Clinique
{
    public partial class AuthentificationForm : Form
    {
        public static String Licence = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Ressources", "login.lic");
        public static String LicencePreCryptage = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Ressources", "loginPreCryptage.lic");

        private BindingSource PatientBindingSource = new BindingSource();

        public AuthentificationForm()
        {
            InitializeComponent();
        }


        /****************************************************************************************************
         * Methods
         ***************************************************************************************************/

        /****************************************************************************************************
         * Events
         ***************************************************************************************************/
        private void AuthentificationForm_Load(object sender, EventArgs e)
        {
            string strKey = "abc123deaoezdf77";
            string strIv = "abc123deaoezdf77";

            {
                //Singleton.Current.MesIdentifiants = new Identification("maxime.a", "azerty");

                //BinaryFormatter bf = new BinaryFormatter();
                //MemoryStream ms = new MemoryStream();
                //bf.Serialize(ms, Singleton.Current.MesIdentifiants);
                //Byte[] octets = ms.ToArray();
                //Directory.CreateDirectory(Path.GetDirectoryName(Licence));
                //File.WriteAllBytes(LicencePreCryptage, octets);

                //// cryptage du fichier
                //Tools.Libraries.AesRjindael.EncryptFile(strKey, strIv, LicencePreCryptage, Licence);
                //File.Delete(LicencePreCryptage);
            }

            {
                // décryptage du fichier
                Tools.Libraries.AesRjindael.DecryptFile(strKey, strIv, Licence, LicencePreCryptage);

                //-> Chargement du Singleton
                //-> Identifiants
                BinaryFormatter bf = new BinaryFormatter();
                Byte[] bytes = File.ReadAllBytes(LicencePreCryptage);
                MemoryStream ms = new MemoryStream(bytes);
                Object o = bf.Deserialize(ms);
                Singleton.Current.MesIdentifiants = o as Identification;

                File.Delete(LicencePreCryptage);
            }
        }

        private void ConnexionButton_Click(object sender, EventArgs e)
        {
            //-> Hash du mot de passe saisi
            Byte[] motDePasseSaisi = Hash.GetSHA1Hash(MotDePasseTextEdit.Text);

            //-> Comparaison des identifiants
            bool IdentifiantCorrect = (IdentifiantTextEdit.Text == Singleton.Current.MesIdentifiants.Identifiant);
            bool MotDePasseCorrect = (motDePasseSaisi.SequenceEqual(Singleton.Current.MesIdentifiants.MotDePasse));
            bool IdentificationReussie = (IdentifiantCorrect && MotDePasseCorrect);

            //-> Actions
            switch (IdentificationReussie)
            {
                case true:
                    this.Close();
                    FormStart EcranApp = new FormStart();
                    EcranApp.ActiveApp();
                    break;

                case false:
                    IdentifiantTextEdit.Text = string.Empty;
                    MotDePasseTextEdit.Text = string.Empty;

                    switch (IdentifiantCorrect)
                    {
                        case true:
                            if (!MotDePasseCorrect)
                            {
                                ErreurAuthentificationLabel.Text = "Mot de passe incorrect.";
                            }
                            break;

                        case false:
                            ErreurAuthentificationLabel.Text = "Identifiant incorrect.";
                            break;
                    }

                    break;
            }
        }


        //private void AnnulerButton_Click(object sender, EventArgs e)
        //{
        //    this.Close();
        //}
    }
}
